import 'package:flutter/material.dart';

import '../../common/system_config.dart';
import '../../utils/tools/screenutil_tool.dart';

class AppBarWidget {

  final String title;
  final double et;
  final bool ct;
  final IconData rightIcon;
  final bool isShowRI;

  /// 状态机
  /// 触发条件
  /// 1、启用isShowRI
  /// 2、pushNamed跳转方式
  /// [beforeCallback] 路由跳转前触发的作用
  final Function beforeCallback;

  /// [afterReturningCallBack] 再次返回界面后触发的作用
  final Function afterReturningCallBack;

  /// AppBar
  /// * [title] 必填|选填|String|默认：AppName
  /// * [et] 投影|选填|double|默认值：5
  /// * [ct] 标题居中|选填|String|默认值：false
  /// * [isShowRI] 是否右侧操作按钮|选填|默认值：false
  /// * [rightIcon] 右侧操作按钮icon|选填|默认值：Icons.settings
  /// * [beforeCallback] 路由跳转前触发的作用|选填
  /// * [afterReturningCallBack] 再次返回界面后触发的作用
  AppBarWidget(this.title,
      {this.et = 0,
        this.ct = false,
        this.rightIcon = Icons.settings,
        this.isShowRI = true,
        this.beforeCallback = null,
        this.afterReturningCallBack = null });

  PreferredSizeWidget build(BuildContext context) {
    AppBar ab;
    if (isShowRI) {
      ab = AppBar(
          backgroundColor: Theme.of(context).primaryColor,
          title: Text(title, style: TextStyle(fontSize: ST.setsp(38))),
          elevation: et,
          centerTitle: ct,
          actions: [
            PopupMenuButton<String>(
              icon: Icon(Icons.more_horiz),
              itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
                SVBox.SelectView(Icons.swap_horiz, '切换台区', 'a'),
              ],
              onSelected: (String action) {
                switch (action) {
                  case 'a':
                    Navigator.pushNamedAndRemoveUntil(context, '/selectionareaview', (_) => false);
                  break;
                }
              },
            )
          ]);
    } else {
      ab = AppBar(
          backgroundColor: Theme.of(context).primaryColor,
          title: Text(title, style: TextStyle(fontSize: ST.setsp(38))),
          elevation: et,
          centerTitle: ct);
    }
    return ab;
  }
}


class SVBox {
  static SelectView(IconData icon, String text, String id) {
    return PopupMenuItem<String>(
        value: id,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            Icon(icon, color: SystemConfig.themeColor),
            Text(text),
          ],
        ));
  }
}
